﻿Imports PadovadoAPI
Imports System.Data
Imports System.IO


Partial Class AdminAggiungiEvento
    Inherits System.Web.UI.Page

    Public Property PK_EventoCorrente As Integer
        Set(value As Integer)
            ViewState("PK_EventoCorrente") = value
        End Set
        Get
            If IsNothing(ViewState("PK_EventoCorrente")) Then ViewState("PK_EventoCorrente") = New Evento
            Return ViewState("PK_EventoCorrente")
        End Get
    End Property

   

    Public Property ImgCorrente As String
        Set(value As String)
            ViewState("ImgCorrente") = value
        End Set
        Get
            Return ViewState("ImgCorrente")
        End Get
    End Property

    Public Property SqlGlobal As String
        Set(value As String)
            ViewState("SqlGlobal") = value
        End Set
        Get
            If String.IsNullOrEmpty(ViewState("SqlGlobal")) Then
                ViewState("SqlGlobal") = "SELECT PK_EventoPeriodico, Nome, Descrizione, Giorno FROM EventoPeriodico WHERE Deleted = 0 ORDER BY Nome desc"
            End If
            Return ViewState("SqlGlobal")
        End Get
    End Property


    Public Function GetUploadedFile(FileUp As FileUpload, LblUp As Label, folder As String) As String
        Dim retStr As String = ""
        If FileUp.HasFile Then
            Try
                Dim di As DirectoryInfo = New DirectoryInfo(Server.MapPath("/"))
                FileUp.SaveAs(di.FullName & "/" & folder & "/" & FileUp.FileName)
                retStr = "Http://www.padovado.it/" & folder & "/" & FileUp.FileName
                LblUp.Text = "File name: " & FileUp.PostedFile.FileName & " " & "File Size: " & _
                                            FileUp.PostedFile.ContentLength & " kb " & _
                                            "Content type: " & FileUp.PostedFile.ContentType
            Catch ex As Exception
                LblUp.Text = "ERRORE nel caricamento: " & ex.Message.ToString()
            End Try
        Else
            LblUp.Text = "Nessuna Brochure allegata"
        End If
        Return retStr
    End Function


    Protected Sub BtnSalva_Click(sender As Object, e As System.EventArgs) Handles BtnSalva.Click

        Dim sql As String = ""
        Dim dbo As New DBObject

        Dim evt As New EventoPeriodico
        evt.LoadFromPk(PK_EventoCorrente)

     
        evt.Nome = TBNomeEvento.Text
        evt.Descrizione = TBDescrizione.Text
        evt.Giorno = CBGiorni.SelectedValue

      
        Dim ImgLink As String = ""
        ImgLink = GetUploadedFile(FUImmagineEvento, LblUploadedImmagine, "eventimages")
        If String.IsNullOrEmpty(ImgLink) Then ImgLink = ImgCorrente
        
        evt.ImgEvento = ImgLink
      
        If IsNothing(evt.FK_Indirizzo) Then evt.FK_Indirizzo = New Indirizzo
        evt.FK_Indirizzo.Strada = TBStrada.Text
        evt.FK_Indirizzo.Civico = TBCivico.Text
        evt.FK_Indirizzo.CAP = TBCAP.Text
        evt.FK_Indirizzo.Comune = TBComune.Text
        evt.FK_Indirizzo.Provincia = TBProvincia.Text
        evt.FK_Indirizzo.Presso = TBPresso.Text
    
        evt.Salva()

        caricaGrid(SqlGlobal)
    End Sub


    Protected Sub BTNInserisci_Click(sender As Object, e As System.EventArgs) Handles BTNInserisci.Click
        Dim sql As String = ""
        Dim dbo As New DBObject
        Dim PK_Indirizzo As Integer
      
        Try

            Dim ImgLink As String = ""
            ImgLink = GetUploadedFile(FUImmagineEvento, LblUploadedImmagine, "eventimages")

            sql = "INSERT INTO EventoPeriodico (Nome, Descrizione, giorno, FK_Indirizzo, ImgEvento, Deleted) VALUES ('" & _
                                            TBNomeEvento.Text.Replace("'", "''") & "', '" &
                                            TBDescrizione.Text.Replace("'", "''") & "', " &
                                            CBGiorni.SelectedValue & ", " &
                                            PK_Indirizzo & ", '" &
                                            ImgLink & "', 0)"
            dbo.ExecuteNonQuery(sql, Nothing, CommandType.Text)

           caricaGrid(SqlGlobal)

        Catch ex As Exception
            Throw New Exception(sql)
        End Try
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If String.IsNullOrEmpty(Session("UserInSession")) Then
            Response.Redirect("Admin.aspx")
        End If
        Dim sql As String = ""
        Dim dbo As New DBObject
        Dim ds As New DataSet
        If Not IsPostBack Then


            caricaGrid(SqlGlobal)

        End If

    End Sub

    Public Sub caricaGrid(sql As String)

        Dim ds As New DataSet
        Dim dbo As New DBObject
        ds = dbo.FillDS(sql, Nothing, CommandType.Text)
        If Not IsNothing(ds) AndAlso (ds.Tables.Count > 0) AndAlso ds.Tables(0).Rows.Count > 0 Then
            GVEventi.DataSource = ds.Tables(0)
            GVEventi.DataBind()
        End If
    End Sub

    Public Sub caricaRiga(ev As EventoPeriodico)
        TBNomeEvento.Text = ev.Nome
        TBDescrizione.Text = ev.Descrizione
        'Throw New Exception(ev.Descrizione & " " & ev.NomeEvento)
        If ev.Giorno > 0 Then
            CBGiorni.SelectedValue = ev.Giorno
        End If

        ImgCorrente = ev.ImgEvento
      
       
        If Not IsNothing(ev.FK_Indirizzo) Then
            TBStrada.Text = ev.FK_Indirizzo.Strada
            TBCivico.Text = ev.FK_Indirizzo.Civico
            TBCAP.Text = ev.FK_Indirizzo.CAP
            TBComune.Text = ev.FK_Indirizzo.Comune
            TBProvincia.Text = ev.FK_Indirizzo.Provincia
            TBPresso.Text = ev.FK_Indirizzo.Presso
        End If
      
    End Sub

    Protected Sub GVEventi_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GVEventi.RowCommand
        If Not IsNothing(e) Then

            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = GVEventi.Rows(index)
            PK_EventoCorrente = row.Cells(0).Text

            If e.CommandName = "CaricaRiga" Then


                Dim ev As New EventoPeriodico
                ev.LoadFromPk(PK_EventoCorrente)
                caricaRiga(ev)
            ElseIf e.CommandName = "EliminaRiga" Then

                EventoPeriodico.CancellaEvento(PK_EventoCorrente)

            End If
        End If

    End Sub

    Protected Sub GVEventi_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles GVEventi.PageIndexChanging

        GVEventi.PageIndex = e.NewPageIndex

        caricaGrid(SqlGlobal)
    End Sub

    Protected Sub BtnSvuota_Click(sender As Object, e As System.EventArgs) Handles BtnSvuota.Click
       
        TBNomeEvento.Text = ""
        TBDescrizione.Text = ""
        CBGiorni.SelectedValue = 1
     
        TBStrada.Text = ""
        TBCivico.Text = ""
        TBComune.Text = ""
        TBCAP.Text = ""
        TBProvincia.Text = ""
        TBPresso.Text = ""
   
        PK_EventoCorrente = 0
     
    End Sub

    Protected Sub LinkButton1_Click(sender As Object, e As System.EventArgs) Handles LinkButton1.Click
        Session("UserInSession") = Nothing
        Response.Redirect("Default.aspx")
    End Sub


    Protected Sub BtnCerca_Click(sender As Object, e As System.EventArgs) Handles BtnCerca.Click
        Dim Sql As String = "SELECT PK_EventoPeriodico, Nome, Descrizione, Giorno FROM EventoPeriodico E " & _
                " INNER JOIN Indirizzo I ON E.FK_Indirizzo = I.PK_Indirizzo " & _
                 " WHERE E.Deleted = 0 AND (E.Nome like '%" & TbToSearch.Text.Replace(" ", "%") & "%' " & _
                 " OR E.Descrizione like '%" & TbToSearch.Text.Replace(" ", "%") & "%' " & _
                 " OR isnull(I.Strada,'') + ' ' + isnull(I.Civico,'') + ' ' + isnull(I.Comune,'') + ' ' + isnull(I.Presso,'') like '%" & TbToSearch.Text.Replace(" ", "%") & "%') " & _
                 " ORDER BY PK_EventoPeriodico desc"
        caricaGrid(Sql)
    End Sub

    Protected Sub BtnMostratutto_Click(sender As Object, e As System.EventArgs) Handles BtnMostratutto.Click
        caricaGrid(SqlGlobal)
    End Sub
End Class

